#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#include<time.h>
#include<sys/times.h>
#include<sys/time.h>
 
#define	BUF_SIZE	512
#define	FILE_NAME	"demo.txt"
#define	err_exit(m)	{perror(m); exit(1);}
	
int	main(){
	int	i,fd,clocks_per_sec;	//文件句柄和每秒时钟数
	char	buf[BUF_SIZE];		//文件缓冲区大小
	struct	tms st_tms;		//times函数的机构
	clock_t start = clock();	//记录起始时间，
	fd = open(FILE_NAME, O_RDONLY | O_CREAT, 0664);
	if(fd == -1) err_exit("open error");
	while(read(fd, buf, BUF_SIZE) != 0);
	for(i = 0; i < 1000000000; i++);
	if (times(&st_tms) == -1) err_exit("times error");
	clock_t end = clock();
	clocks_per_sec = sysconf(_SC_CLK_TCK);	//用于获得times函数的单位时钟数
	printf("real  times: %7dus\n", (end-start)/CLOCKS_PER_SEC*1000000);
	printf("user  times: %7dus\n", st_tms.tms_utime*1000000/(clocks_per_sec));
	printf("syst  times: %7dus\n", st_tms.tms_stime*1000000/(clocks_per_sec));
	return 0;
}
